﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using PiscoSourDominio;
using PiscoSourSOAPRestServices.Persistencia;
using Dapper;

namespace PiscoSourPersistencia
{
    public class CartaLocalDAO
    {
        public string InsertarCartaLocal(Carta_Local cartaLocal)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {


                    sqlConnection.Open();
                    string sqlQuery = "INSERT INTO PSR_CARTA_LOCAL(intCodPlato,intCodLocal,decPrecio) VALUES (@intCodPlato,@intCodLocal,@decPrecio)";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCodPlato = cartaLocal.intCodPlato,
                            intCodLocal = cartaLocal.intCodLocal,
                            decPrecio = cartaLocal.decPrecio
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string ActualizarCartaLocal(Carta_Local cartaLocal)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {
                    sqlConnection.Open();
                    string sqlQuery = "UPDATE PSR_CARTA_LOCAL SET intCodPlato=@intCodPlato,intCodLocal=@intCodLocal,decPrecio=@decPrecio WHERE idCodCarta=@idCodCarta";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            idCodCarta = cartaLocal.idCodCarta,
                            intCodPlato = cartaLocal.intCodPlato,
                            intCodLocal = cartaLocal.intCodLocal,
                            decPrecio = cartaLocal.decPrecio
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string EliminarCartaLocal(Carta_Local cartaLocal)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "DELETE FROM PSR_CARTA_LOCAL WHERE idCodCarta=@idCodCarta";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            idCodCarta = cartaLocal.idCodCarta
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public Carta_Local ObtenerCartaLocal(string codigo)
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                const string sqlQuery = "select * from psr_carta_local where idCodCarta=@idCodCarta";
                var cartaLocal = sqlConnection.Query<Carta_Local>(sqlQuery, new { idCodCarta = codigo }).FirstOrDefault<Carta_Local>();

                sqlConnection.Close();

                return cartaLocal;
            }
        }

        public IEnumerable<Carta_Local> ObtenerListaCartaLocal(string codigo)
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                const string sqlQuery = "select * from psr_carta_local where intCodLocal=@intCodLocal";
                var cartaLocal = sqlConnection.Query<Carta_Local>(sqlQuery, new { intCodLocal = codigo });
                sqlConnection.Close();

                return cartaLocal;
            }
        }

        public IEnumerable<Carta_Local> ListarCartasLocales()
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                var sqlQuery = string.Format(@"Select * from psr_carta_local");
                var result = sqlConnection.Query<Carta_Local>(sqlQuery);
                sqlConnection.Close();
                return result;
            }
        }
    }
}